﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using DynamicComparer;
using Shuba.Model;

namespace Shuba.Web.master
{
    public partial class parameter_list_old : basePage
    {
        string msg = "";
        bool IsView = false, IsAdd = false, IsEdit = false, IsDelete = false, IsChecker = false;

        private void BindData()
        {
            Shuba.Business.AppParam param = new Shuba.Business.AppParam();
            List<AppParamInfo> list;

            list = param.GetList();

            gvList.DataSource = list;
            gvList.DataBind();

            if (IsEdit) gvList.Columns[gvList.Columns.Count - 1].Visible = true; else gvList.Columns[gvList.Columns.Count - 1].Visible = false;
            
            //FOR APPROVAL GRID
            if (IsChecker)
            {
                BindApprovalData();

                if (ApprovalGrid.Rows.Count > 0) { ApprovalPnl.Visible = true; } else { ApprovalPnl.Visible = false; }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            string acl = GetUserAccess("MSTR_PARAM");

            IsView = IsUserView(acl);
            IsAdd = IsUserAdd(acl);
            IsEdit = IsUserEdit(acl);
            IsDelete = IsUserDelete(acl);
            IsChecker = IsUserChecker(acl);

            if (!IsView)
            {
                msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
            }

            if (!IsPostBack)
            {
                InitSort();
                BindData();

                LabelTitle.Text = GetPageTitle("MSTR_PARAM");

            }
        }

        protected void gvList_RowCommand(Object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edit") Response.Redirect("parameter_add.aspx?a=ed&id=" + e.CommandArgument);
        }

        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvList.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortDirection(e.SortExpression,e.SortDirection.ToString());
            ViewState["SortEx"] = e.SortExpression;
            gvList.PageIndex = 0;
            BindData();
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            Shuba.Framework.Encryption Encryption = new Shuba.Framework.Encryption(); 
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton btnEdit = (LinkButton)e.Row.FindControl("btnEdit");
                if(IsEdit) btnEdit.Enabled = true; else btnEdit.Enabled = false;
                //e.Row.Cells[4].Text = Encryption.Decrypt(e.Row.Cells[4].Text, Encryption.passPhrase, Encryption.saltValue, Encryption.hashAlgorithm, Encryption.passwordIterations, Encryption.initVector, Encryption.keySize);
                //e.Row.Cells[5].Text = Encryption.Decrypt(e.Row.Cells[5].Text, Encryption.passPhrase, Encryption.saltValue, Encryption.hashAlgorithm, Encryption.passwordIterations, Encryption.initVector, Encryption.keySize);
            }
        }

        protected void ApprovalGrid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //Shuba.Framework.Encryption Encryption = new Shuba.Framework.Encryption();
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    e.Row.Cells[5].Text = Encryption.Decrypt(e.Row.Cells[5].Text, Encryption.passPhrase, Encryption.saltValue, Encryption.hashAlgorithm, Encryption.passwordIterations, Encryption.initVector, Encryption.keySize);
            //}
        }

        /*======================================
                APPROVAL GRID START HERE
         =======================================
         */
        private void BindApprovalData()
        {
            Shuba.Business.AppParam param = new Shuba.Business.AppParam();
            List<AppParamInfo> list;

            list = param.GetListApproval(HttpContext.Current.User.Identity.Name);
            if (ViewState["SortEx"].ToString().Trim() != "")
            {
                string sort = sort = ViewState["SortEx"].ToString() + " " + ViewState["Sort"];
                DynamicComparer<AppParamInfo> comparer = new DynamicComparer<AppParamInfo>(sort);
                list.Sort(comparer);
            }
            ApprovalGrid.DataSource = list;
            ApprovalGrid.DataBind();
        }

        protected void ApprovalGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            ApprovalGrid.PageIndex = e.NewPageIndex;
            BindApprovalData();
        }

        protected void ApprovalGrid_Sorting(object sender, GridViewSortEventArgs e)
        {
            SortDirection(e.SortExpression, e.SortDirection.ToString());
            ViewState["SortEx"] = e.SortExpression;
            ApprovalGrid.PageIndex = 0;
            BindApprovalData();
        }

        protected void btnApprove_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in ApprovalGrid.Rows)
            {
                CheckBox chkItemSelect = (CheckBox)row.FindControl("chkItemSelect");
                string id = Convert.ToString(ApprovalGrid.DataKeys[row.RowIndex].Value.ToString());
                if (chkItemSelect.Checked)
                {
                    Approve(id, true);
                }
            }
            msg = this.GetGlobalResourceObject("Messages", "ApproveAlert").ToString();
            BindData();
            Alert(msg);

            //AlertAndRedirect(msg, Request.Url.PathAndQuery);
        }

        protected void btnReject_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow row in ApprovalGrid.Rows)
            {
                CheckBox chkItemSelect = (CheckBox)row.FindControl("chkItemSelect");
                string id = Convert.ToString(ApprovalGrid.DataKeys[row.RowIndex].Value.ToString());
                if (chkItemSelect.Checked)
                {
                    Approve(id, false);
                }
            }
            msg = this.GetGlobalResourceObject("Messages", "NoApprove").ToString();
            BindData();
            Alert(msg);

            //AlertAndRedirect(msg, Request.Url.PathAndQuery);
        }

        private void Approve(string id, bool IsApprove)
        {
            string ActType;

            Shuba.Business.AppParam param = new Shuba.Business.AppParam();
            Shuba.Model.AppParamInfo bi = new Shuba.Model.AppParamInfo();

            bi = param.GetApprovalByID(Convert.ToByte(id));
            ActType = bi.ACT_TYPE;
            bi.UPDATE_BY = HttpContext.Current.User.Identity.Name;
            bi.UPDATE_DATE = ProcessingDate();

            if (IsApprove)
            {
                param.Approve(Convert.ToByte(id), ActType);
                AddLog("APPROVAL", "APPROVE SYSTEM PARAMETER");
            }
            else
            {
                param.Reject(Convert.ToByte(id));
                AddLog("APPROVAL", "REJECT SYSTEM PARAMETER");
            }
        }
    }   
 }

